import {Component, OnInit} from '@angular/core';
import {NavController, ModalController} from 'ionic-angular';
import {StoragePage} from "../storage/storage";
import {PopOverPage} from "../popoverPage/popoverPage";
import {BarcodeScanner} from "@ionic-native/barcode-scanner";
import {ResultPage} from "../result/result";
import {services} from "../../../providers/service";
import {AppPopup} from "../../../util/popup";
import {StorageService} from "../../../providers/storage-service";

@Component({
    selector: 'newCreat', templateUrl: 'newcreat.html'
})
export class NewCreatPage implements OnInit {
    showAlert = true;
    index = 0;
    product = {
        productId: "", productImg: "", productName: ""
    };
    productList = {
        productId: "", productImg: "", productName: ""
    };

    thisProductInfo;           //新建出库单返回的数据
    constructor(public navCtrl: NavController,
                public modalCtrl: ModalController,
                public barcodeScanner: BarcodeScanner,
                public service: services,
                private popup: AppPopup,
                private storageServ: StorageService,) {

    }

    presentProfileModal() {
        let profileModal = this.modalCtrl.create(PopOverPage, {productList: this.productList});
        profileModal.onDidDismiss(index => {
            console.log(index);
            if (index) {
                this.index = index;
                this.product = this.productList[this.index];
            }
        })
        profileModal.present();
    }

    toShowAlert() {
        this.showAlert = false;
    }

    btnCancel() {
        this.showAlert = true;
    }

    btnChoose() {
        this.showAlert = true;
        this.navCtrl.popToRoot(StoragePage);
    }

    toAdd() {
        let param = {
            "productId": this.product.productId
        }
        this.storageServ.add(param)
            .then(res => {
                console.info("新建出库单成功：", res);
                this.navCtrl.popToRoot(StoragePage);
            })
            .catch(err => {
                console.error("新建出库单失败：", err);
                this.popup.alert(err.errorMassage);
            });
    }

    ngOnInit() {
        this.storageServ.productLists()
            .then(res => {
                console.info("获取产品列表成功：", res);
                if (res.infoData instanceof Array && res.infoData.length) {
                    this.productList = res.infoData;
                    this.product = res.infoData[0];
                } else {
                    this.popup.alert("你没有可出库的产品");
                }
            })
            .catch(err => {
                console.error("获取产品列表失败：", err);
                this.popup.alert(err.errorMassage);
            });
    }

    toScan() {
        let param = {
            "productId": this.product.productId
        }
        this.storageServ.add(param)
            .then(res => {
                console.info("新建出库单成功：", res);
                this.thisProductInfo = res.infoData;
                //继续扫码
                this.barcodeScanner.scan().then((barcodeData) => {
                    console.log(barcodeData.text);
                    if (barcodeData.text == null || barcodeData.text == '') {
                        console.log('取消了扫码');
                        return;
                    }
                    this.service.boxNo = barcodeData.text;
                    let param = {
                        "boxNo": barcodeData.text,        //箱码
                        "outId": this.thisProductInfo.id,        //出库单ID
                        "outNo": this.thisProductInfo.outNo,     //出库单号
                        "productId": this.product.productId,     //产品ID
                    }
                    this.thisProductInfo.productImg = this.product.productImg;
                    this.thisProductInfo.productId = this.product.productId;
                    this.storageServ.scan(param)
                        .then(res => {
                            console.info("出库单_出库扫码成功：", res);
                            this.navCtrl.push(ResultPage, {data: res, thisProductInfo: this.thisProductInfo});
                        })
                        .catch(err => {
                            console.error("出库单_出库扫码失败：", err);
                            this.navCtrl.push(ResultPage, {data: err, thisProductInfo: this.thisProductInfo});
                        });
                }, (err) => {
                    // An error occurred
                    console.error(err);
                });
            })
            .catch(err => {
                console.error("新建出库单失败：", err);
                this.popup.alert(err.errorMassage);
            });
    }
}
